/**
 * 
 */
$(document)
		.ready(
				function() {

					$("#group-pane-outer").hide();
					$("#user-pane-outer").hide();

					$(".personal_links").click(
							function() {
								dropdown_click = true;
								if (already_dropdown == false) {
									$(".personal_links .dropdown").css("top",
											"40px");
									already_dropdown = true;
								} else {
									$(".personal_links .dropdown").css("top",
											"-999px");
									already_dropdown = false;
								}
							});

					$("#page").click(
							function() {
								if (already_dropdown == true
										&& dropdown_click == false) {
									$(".personal_links .dropdown").css("top",
											"-999px");
									already_dropdown = false;
								}
								dropdown_click = false;
							});

					$("#global-nav-home").click(function() {
						if (currentPage == page[0])
							return;
						else {
							$("#page-out-profile").fadeOut();
							$("#page-out-messages").fadeOut();
							$("#page-out-home").slideDown();

							$("#global-nav-home").addClass("active");
							$("#global-nav-profile").removeClass("active");
							$("#global-nav-messages").removeClass("active");

							currentPage = page[0];
						}
					});

					$("#global-nav-profile").click(function() {
						if (currentPage == page[1])
							return;
						else {
							$("#page-out-home").fadeOut();
							$("#page-out-messages").fadeOut();
							$("#page-out-profile").slideDown();

							$("#global-nav-home").removeClass("active");
							$("#global-nav-profile").addClass("active");
							$("#global-nav-messages").removeClass("active");

							$("#well").fadeIn();
							is_profile_pushed = false;

							currentPage = page[1];
						}
					});

					$("#global-nav-messages").click(function() {
						if (currentPage == page[2])
							return;
						else {
							$("#page-out-profile").fadeOut();
							$("#page-out-home").fadeOut();
							$("#page-out-messages").slideDown();

							$("#global-nav-home").removeClass("active");
							$("#global-nav-profile").removeClass("active");
							$("#global-nav-messages").addClass("active");

							currentPage = page[2];
						}
					});

					$("#screen-name").mouseover(function() {
						$("#screen-name").css("color", "#FFFFFF");
					});
					$("#screen-name").mouseout(function() {
						$("#screen-name").css("color", "#BABABA");
					});

					$("#personal_links_setting").mouseover(function() {
						$("#personal_links_setting").addClass("active");
						$("#personal_links_setting a").css("color", "#FFFFFF");
					});
					$("#personal_links_setting").mouseout(function() {
						$("#personal_links_setting").removeClass("active");
						$("#personal_links_setting a").css("color", "#BABABA");
					});

					$("#personal_links_help").mouseover(function() {
						$("#personal_links_help").addClass("active");
						$("#personal_links_help a").css("color", "#FFFFFF");
					});
					$("#personal_links_help").mouseout(function() {
						$("#personal_links_help").removeClass("active");
						$("#personal_links_help a").css("color", "#BABABA");
					});

					$("#personal_links_signout").mouseover(function() {
						$("#personal_links_signout").addClass("active");
						$("#personal_links_signout a").css("color", "#FFFFFF");
					});
					$("#personal_links_signout").mouseout(function() {
						$("#personal_links_signout").removeClass("active");
						$("#personal_links_signout a").css("color", "#BABABA");
					});

					$("#group-stream-manager").show();

					$("a.close, #fade").live("click", function() { // When
						// clicking
						// on
						// the close or fade
						// layer...
						$('#fade , .popup_block').fadeOut(function() {
							$('#fade, a.close').remove(); // fade them
							// both out
						});
						return false;
					});

					// click the reply button
					$("#reply-button").click(
							function() {
								var text = $("#popup_reply textarea").val();
								var id = userID;
								var remind_item_id = $("#reply-button").attr(
										"remind-item-id");
								$.ajax({
									type : "POST",
									url : "servlet/SetReply",
									data : "type=reply" + "&id=" + id
											+ "&reply=" + text + "&remindid="
											+ currentRemindId,
									success : function(msg) {
										if (msg == "success") {
											$("#popup_reply").fadeOut();
											$("#" + remind_item_id).fadeOut();
											jNotify("success.", {
												HorizontalPosition : "center",
												VerticalPosition : "center"
											});
										} else
											jError("error.", {
												HorizontalPosition : "center",
												VerticalPosition : "center"
											});
									}
								});
								return false;
							});
					
					//send message
					$("#message-button").click(
							function() {
								var text = $("#popup_message textarea").val();
								var title = $("#popup_message input").val();
								var id = userID;
								var message_user_id = $("#message-button").attr(
										"message-user-id");
								$.ajax({
									type : "POST",
									url : "servlet/SetMessage",
									data : "type=message" + "&id=" + id
											+ "&message=" + text + "&usertoid="
											+ message_user_id+"&title="+title,
									success : function(msg) {
										if (msg == "success") {
											$("#popup_message").fadeOut();
											jNotify("success.", {
												HorizontalPosition : "center",
												VerticalPosition : "center"
											});
										} else
											jError("error.", {
												HorizontalPosition : "center",
												VerticalPosition : "center"
											});
									}
								});
								return false;
							});
					
					

					$("#new-group-button")
							.click(
									function() {
										var popID = "popup_new_group"; // Get
										// Popup
										// Name
										var popURL = "#?w=500"; // Get Popup
										var query = popURL.split('?');
										var dim = query[1].split('&');
										var popWidth = dim[0].split('=')[1]; // Gets
										// the
										// first
										// query
										// string value

										// Fade in the Popup and add close
										// button
										$('#' + popID)
												.fadeIn()
												.css(
														{
															'width' : Number(popWidth) - 50
														})
												.prepend(
														'<a href="#" class="close"><img src="res/pic/home/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

										// Define margin for center
										// alignment (vertical horizontal) -
										// we add 80px to the height/width
										// to accomodate for the padding and
										// border width defined in the css
										var popMargTop = ($('#' + popID)
												.height() + 80) / 2;
										var popMargLeft = ($('#' + popID)
												.width() + 80) / 2;

										// Apply Margin to Popup
										$('#' + popID).css({
											'margin-top' : -popMargTop,
											'margin-left' : -popMargLeft
										});

										$(".twitter-anywhere-tweet-box-editor")
												.val(
														"@"
																+ $(
																		"#"
																				+ id
																				+ " .message-name .message-nickname")
																		.text()
																+ " ");
										currentRemindId = $("#" + id).attr(
												"remind-id");
										$("#reply-button").attr(
												"remind-item-id", id);

										return false;

									});

					$(".stream-tab").click(
							function() {
								var tabs = new Array("tab-groups",
										"tab-people", "tab-favorites");
								var managers = new Array(
										"group-stream-manager",
										"user-stream-manager",
										"favorites-stream-manager");
								var id = $(this).attr("id");
								for (x in tabs) {
									if (tabs[x] == id) {
										$("#" + tabs[x]).addClass(
												"current-focus");
										$("#" + managers[x]).fadeIn();
									} else {
										$("#" + tabs[x]).removeClass(
												"current-focus");
										$("#" + managers[x]).fadeOut();
									}
								}
								return false;
							});

					// group filter
					$("#group-navigation-input")
							.keyup(
									function() {

										var value = $("#group-navigation-input")
												.val();

										var regexp = new RegExp(value
												.toLowerCase());

										for ( var i = 0; i < 10000; i++) {
											var item = $(
													"#group-stream-manager .group-item")
													.eq(i);
											var groupname = $(item).find(
													".group-name").text();
											if (groupname == "")
												break;
											else if (!groupname.toLowerCase()
													.match(regexp)) {
												$(
														"#group-stream-manager .group-item")
														.eq(i).fadeOut();
											} else {
												$(
														"#group-stream-manager .group-item")
														.eq(i).fadeIn();
											}
										}
									});

					$("#group-trash-button")
							.click(
									function() {
										$("#group-navigation-input").val("");
										for ( var i = 0; i < 10000; i++) {
											var item = $(
													"#group-stream-manager .group-item")
													.eq(i);
											var groupname = $(item).find(
													".group-name").text();
											if (groupname == "")
												break;
											else {
												$(
														"#group-stream-manager .group-item")
														.eq(i).fadeIn();
											}
										}
									});

					// user filter
					$("#user-navigation-input").keyup(
							function() {

								var value = $("#user-navigation-input").val();

								var regexp = new RegExp(value.toLowerCase());

								for ( var i = 0; i < 10000; i++) {
									var item = $(
											"#user-stream-manager .user-item")
											.eq(i);
									var username = $(item).find(
											".user-nickname").text();
									if (username == "")
										break;
									else if (!username.toLowerCase().match(
											regexp)) {
										$("#user-stream-manager .user-item")
												.eq(i).fadeOut();
									} else {
										$("#user-stream-manager .user-item")
												.eq(i).fadeIn();
									}
								}
							});

					$("#user-trash-button").click(
							function() {
								$("#user-navigation-input").val("");
								for ( var i = 0; i < 10000; i++) {
									var item = $(
											"#user-stream-manager .user-item")
											.eq(i);
									var username = $(item).find(
											".user-nickname").text();
									if (username == "")
										break;
									else {
										$("#user-stream-manager .user-item")
												.eq(i).fadeIn();
									}
								}
							});

					// profile page ajax check
					$("#_user_nickname")
							.focusout(
									function() {

										var nickname = $("#_user_nickname")
												.val();
										$("#user_nickname_field .spinner")
												.show();

										$
												.ajax({
													type : "POST",
													url : "servlet/Profile",
													data : "id="
															+ userID
															+ "&type=nickname&data="
															+ nickname,
													success : function(msg) {
														$(
																"#user_nickname_field .spinner")
																.hide();
														if (msg == "error") {
															$(
																	"#user_nickname_field .taken")
																	.show();
														} else {
															$(
																	"#user_nickname_field .error")
																	.hide();
														}
													}
												});

									});

					$("#_user_password")
							.focusout(
									function() {

										var password = $("#_user_password")
												.val();
										$("#user_password_field .spinner")
												.show();

										$
												.ajax({
													type : "POST",
													url : "servlet/Profile",
													data : "id="
															+ userID
															+ "&type=password&data="
															+ password,
													success : function(msg) {
														$(
																"#user_password_field .spinner")
																.hide();
														if (msg == "error") {
															$(
																	"#user_password_field .incorrect")
																	.show();
														} else {
															$(
																	"#user_password_field .error")
																	.hide();
														}
													}
												});

									});

					$("#more-group-button")
							.click(
									function() {

										$
												.ajax({
													type : "GET",
													url : "servlet/RandomGroups",
													data : "id=" + userID,
													success : function(msg) {

														if (msg == "error") {
															jError(
																	"error.",
																	{
																		HorizontalPosition : "center",
																		VerticalPosition : "center"
																	});
														}
														{
															$(
																	"#popup_more_group ol")
																	.empty();

															var groups = eval("("
																	+ msg + ")");

															for ( var i = 0; i < groups.count; i++) {
																var group = groups.group[i];
																var html = "<li class='tag' id='more-group-"
																		+ group.id
																		+ "' "
																		+ "> <div > <a "
																		+ " title='简介:"
																		+ group.intro
																		+ "&#10; 作者:"
																		+ group.owner
																		+ "&#10; 创建于:"+ group.time+"' onclick='more_FollowGroup("+group.id+")'> <strong>"
																		+ group.name
																		+ "</strong> </a></div>"
																		+ " </li>";

																$(
																		"#popup_more_group ol")
																		.append(
																				html);

															}
														}
													}
												});

										var popID = "popup_more_group"; // Get
										var popURL = "#?w=500"; // Get Popup
										var query = popURL.split('?');
										var dim = query[1].split('&');
										var popWidth = dim[0].split('=')[1]; // Gets
										// the
										// first
										// query
										// string value

										// Fade in the Popup and add close
										// button
										$('#' + popID)
												.fadeIn()
												.css(
														{
															'width' : Number(popWidth) - 50
														})
												.prepend(
														'<a href="#" class="close"><img src="res/pic/home/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

										// Define margin for center
										// alignment (vertical horizontal) -
										// we add 80px to the height/width
										// to accomodate for the padding and
										// border width defined in the css
										var popMargTop = ($('#' + popID)
												.height() + 80) / 2;
										var popMargLeft = ($('#' + popID)
												.width() + 80) / 2;

										// Apply Margin to Popup
										$('#' + popID).css({
											'margin-top' : -popMargTop,
											'margin-left' : -popMargLeft
										});

										return false;
									});

					
					$("#more-user-button")
					.click(
							function() {

								$
										.ajax({
											type : "GET",
											url : "servlet/RandomUsers",
											data : "id=" + userID,
											success : function(msg) {

												if (msg == "error") {
													jError(
															"error.",
															{
																HorizontalPosition : "center",
																VerticalPosition : "center"
															});
												}
												{
													$(
															"#popup_more_user ol")
															.empty();

													var users = eval("("
															+ msg + ")");

													for ( var i = 0; i < users.count; i++) {
														var user = users.user[i];
														var html = "<li class='tag' id='more-user-"
																+ user.id
																+ "' "
																+ "> <div > <a "
																+ " title='简介:"
																+ user.intro
																+ "&#10; 真实姓名:"
																+ user.realname
																+"' onclick='more_FollowUser("+user.id+")'> <strong>"
																+ user.nickname
																+ "</strong> </a></div>"
																+ " </li>";

														$(
																"#popup_more_user ol")
																.append(
																		html);

													}
												}
											}
										});

								var popID = "popup_more_user"; // Get
								var popURL = "#?w=500"; // Get Popup
								var query = popURL.split('?');
								var dim = query[1].split('&');
								var popWidth = dim[0].split('=')[1]; // Gets
								// the
								// first
								// query
								// string value

								// Fade in the Popup and add close
								// button
								$('#' + popID)
										.fadeIn()
										.css(
												{
													'width' : Number(popWidth) - 50
												})
										.prepend(
												'<a href="#" class="close"><img src="res/pic/home/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

								// Define margin for center
								// alignment (vertical horizontal) -
								// we add 80px to the height/width
								// to accomodate for the padding and
								// border width defined in the css
								var popMargTop = ($('#' + popID)
										.height() + 80) / 2;
								var popMargLeft = ($('#' + popID)
										.width() + 80) / 2;

								// Apply Margin to Popup
								$('#' + popID).css({
									'margin-top' : -popMargTop,
									'margin-left' : -popMargLeft
								});

								return false;
							});
					
					
					
					
					

				});
